<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;

class Manager extends Model
{
    //
    use SoftDeletes;

    protected $casts=[
        'wx_userinfo'=>'json'
    ];

    protected $dates=['deleted_at'];

    protected $fillable=['name','phone','password','type','company','invitation_code','parent_mid','status','wx_userinfo'];

    /**
     * @param $ids
     * @return array
     * 导出数据
     */
    public function getExportData($type,$ids=null){
        if($type == 'all'){
            $cellDatas = Manager::orderBy('id', 'desc')
                ->get();
        }else {
            $cellDatas = Manager::whereIn('id', explode(',', $ids))
                ->orderBy('id', 'desc')
                ->get();
        }
        //表头
        $cellData[0] = ['用户ID','姓名','电话号码','渠道类型','公司名称','邀请码','注册时间','状态'];

        if(count($cellDatas)) {
            foreach ($cellDatas as $item) {
                if ($item['status'] == 1) {
                    $status = '正常';
                }else {
                    $status = '冻结';
                }
                $cellData[] = [$item['id'], $item->name, $item->phone, $item->company == 1?'公司':'个人', $item->company, $item->invitation_code, $item->created_at, $status];
            }
        }
        return $cellData;
    }
}
